<template>
  <el-card class="userAvatar-container">
    <!-- 面板屑导航 -->
    <el-breadcrumb separator="/" style="margin-bottom: 40px">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>个人中心</el-breadcrumb-item>
      <el-breadcrumb-item>更换头像</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 用来展示头像的盒子 -->
    <div class="avatar-box">
      <img :src="userImg || ImgDefault" alt="" />
    </div>
    <!-- 按钮 -->
    <div class="btns">
      <!-- 文件选择框 -->
      <input
        type="file"
        accept="image/*"
        style="display: none"
        ref="fileRef"
        @change="onFileChange"
      />
      <el-button type="primary" icon="el-icon-plus" @click="chooseBtn"
        >选择图片</el-button
      >
      <el-button
        type="success"
        icon="el-icon-upload"
        :disabled="isDisabled"
        @click="uploadAvatar"
        >上传头像
      </el-button>
    </div>
  </el-card>
</template>

<script>
// 导入默认图片
import ImgDefault from '../../assets/images/avatar.jpg'
import { mapState, mapActions } from 'vuex'
import { reqUpdateAvatar } from '../../api/user.js'

export default {
  name: 'UserAvatar',
  data () {
    return {
      // 头像的图片
      userImg: '',
      ImgDefault,
      isDisabled: true
    }
  },
  watch: {
    userInfo (newValue, oldValue) {
      this.userImg = newValue.user_pic
    }
  },
  computed: {
    ...mapState('user', ['userInfo'])
  },
  async created () {
    await this.getUserInfo()
    this.userImg = this.userInfo.user_pic
  },
  methods: {
    ...mapActions('user', ['getUserInfo']),
    chooseBtn () {
      this.disabled = true
      this.$refs.fileRef.click()
    },
    onFileChange (e) {
      // console.log(e.target.files[0])
      const file = e.target.files[0]
      // 1. 判断是否选择了图像
      if (!file) {
        // 如果用户没选 将默认头像显示和按钮禁用
        this.isDisabled = true
        this.userImg = ImgDefault
        return this.$message.warning('请选择上传的图像')
      }
      // 2. 限制上传文件的大小 1.5M
      if (file.size / 1024 / 1024 > 1.5) {
        return this.$message.warning('图像大小不能超过1.5M')
      }
      // 3. 处理base64格式图片 回显
      //  3.1 使用fileReader文件读取对象 处理base64格式
      const fileReader = new FileReader()
      console.log(fileReader)
      // 3.2 使用fileReader实例读取文件地址readAsDataURL()
      fileReader.readAsDataURL(file)
      // 3.3在上传成功后load事件中,获取文件的base64格式的地址
      fileReader.addEventListener('load', () => {
        console.log(fileReader.result) // base64格式的图片
        this.userImg = fileReader.result
        // 3.4 启用禁用上传的按钮
        this.isDisabled = false
      })
    },
    async uploadAvatar () {
      const res = await reqUpdateAvatar(this.userImg)
      // console.log(res)
      if (res.data.code === 0) {
        this.$message.success(res.data.message)
      } else {
        this.$message.error(res.data.message)
      }
      // 3. 重新获取个人信息
      this.getUserInfo()
      // 4. 禁用按钮
      this.isDisabled = true
    }
  }
}
</script>

<style lang="less" scoped>
.userAvatar-container {
  padding: 0 0 20px 20px;

  .avatar-box {
    img {
      width: 350px;
      height: 350px;
      object-fit: cover;
    }
  }

  .btns {
    padding-top: 10px;
  }
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
